package com.nstar.timeline.components
{
	import com.nstar.base.util.SystemUtils;
	import com.nstar.timeline.components.itemrenderer.WordCupEvent;
	import com.nstar.timeline.vo.TickVO;
	import com.nstar.timeline.vo.TimeLineVO;
	
	import flash.events.KeyboardEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.system.System;
	import flash.ui.Keyboard;
	
	import mx.containers.Canvas;
	import mx.effects.Move;
	import mx.effects.easing.Circular;
	import mx.events.FlexEvent;

	public class TimeLine extends Canvas
	{
		
		public static const DISP_COUNT:Number = 3;
		private var _title:String;
		private var _subTitle:String;
		
		private var _timelineTrack:TimeLineTrack;
		
		private var _trackOffsetY:Number = 0;//track offset y distance
		private var _tickValues:Array;
		
		private var _dataProvider:Array = new Array();//当前显示列表
		private var listRight:Array = new Array();//未显示列表
		private var listLeft:Array = new Array();//已显示列表
		
		private var points:Array = new Array();//当前显示事件对象的位置信息
		
		private var hgap:Number = 300;//水平间距
		
		
		public function TimeLine()
		{
			super();
			this.addEventListener(FlexEvent.CREATION_COMPLETE,init);
		}
		
		private function init(event:FlexEvent):void{
			this.addEventListener(KeyboardEvent.KEY_DOWN,keydownHandler);
			this.setFocus();
		}
		
		override protected function createChildren():void{
			super.createChildren();
			//add Timeline track
			addTimeLineTrack();
		}	
		
		//添加时间轴轨道
		private function addTimeLineTrack():void{
			_timelineTrack = new TimeLineTrack();
			this.addChild(_timelineTrack);
		}
		

		//添加事件对象,xStart:起始位置
		private function addEvent(timelineVO:TimeLineVO,xStart:Number):WordCupEvent{
			//刻度线位置
			var vo:TickVO = new TickVO();
			vo.value = timelineVO.x;
			vo.height = Math.abs(timelineVO.yOffset);
			vo.direction = timelineVO.yOffset>0?TickVO.UP:TickVO.DOWN;
			vo.color = Math.random()*0xFFFFFF;
			
			timelineVO.direction = vo.direction;
			timelineVO.tickVO = vo;
			
			var event:WordCupEvent = new WordCupEvent();
			event.timeLineVO = timelineVO;
			event.x = xStart;
			event.y = 50;
//			switch(timelineVO.direction){
//				case TickVO.UP:
//					event.y = this.centerY-timelineVO.height-_trackOffsetY;
//					break;
//				case TickVO.DOWN:
//					event.y = this.centerY+_trackOffsetY;
//					break;
//			}			
			event.width = timelineVO.width;
			event.height = timelineVO.height;
			
			event.timeLineVO.direction = vo.direction;
			event.dragStrictRect = new Rectangle(0,event.y,this.width-event.width,0);
			
			this.addChild(event);
			return event;
		}		
		
		//打印当前三个列表数据
		public function traceList(direction:String=""):void{
			trace("======="+direction+"=======");
			trace("listLeft="+listLeft);
			trace("current="+_dataProvider);
			trace("listRight="+listRight);
			trace("event count="+events.length);
		}

		public function set dataProvider(value:Array):void{
			//将原始未显示的数据放在未显示列表中
			listRight = value;
			//取出前DISP_COUNT个待显示的数据到待显示列表中
			for(var i:int=0;i<DISP_COUNT;i++){
				_dataProvider.push(listRight.shift());
			}
			
			var tvs:Array = new Array();
			if(this._dataProvider){
				var idx:int = 0;
				var eventList:Array = new Array();
				for each(var timelineVO:TimeLineVO in this._dataProvider){
					//添加事件对象
					var event:WordCupEvent = addEvent(timelineVO,this.width+10);
					//计算初始点位置
					var p:Point = new Point();
					p.x = idx*hgap+10;
					p.y = event.y;
					//保存位置信息
					points.push(p);
					//保存事件对象
					eventList.push(event);
					idx++;
				}
				
				traceList();
				
				//动态移入效果
				for(var i:int = 0;i<eventList.length;i++){
					var m:Move = new Move(eventList[i]);
					m.easingFunction = Circular.easeOut;
					m.startDelay = i*100;
					m.xFrom = WordCupEvent(eventList[i]).x;
					m.yFrom = points[i].y;
					m.xTo = points[i].x;
					m.yTo = points[i].y;
					m.play();
				}
			}
		}
		
		//所有的事件对象
		public function get events():Array{
			var ary:Array = new Array();
			for each(var item:Object in this.getChildren()){
				if(item is WordCupEvent){
					ary.push(item);
				}
			}
			return ary;
		}
		
		//删除所有事件对象
		public function removeAllEvents():void{
			for each(var item:WordCupEvent in events){
				this.removeChild(item);
			}
		}
		
		//取得指定索引的事件对象
		public function getEventByIndex(index:int):WordCupEvent{
			for each(var event:WordCupEvent in events){
				if(event.timeLineVO.index == index){
					return event;
				}
			}
			return null;
		}
		
		public function get dataProvider():Array{
			return this._dataProvider;
		}
		
		public function set title(value:String):void{
			this._title = value;
		}
		
		public function get title():String{
			return this._title;
		}
		
		public function set subTitle(value:String):void{
			this._subTitle = value;
		}
		
		public function get subTitle():String{
			return this._subTitle;
		}
		
		public function get centerY():Number{
			return this.height/2;
		}

		public function toXML():String{
			var xml:String = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"+"\n";
			xml += "<TimeLine>"+"\n";
			xml += SystemUtils.addTab()+"<Header>"+"\n";
			xml += SystemUtils.addTab(2)+	"<Title>"+_title+"</Title>"+"\n";
			xml += SystemUtils.addTab(2)+	"<SubTitle>"+_subTitle+"</SubTitle>"+"\n";
			xml += SystemUtils.addTab()+"</Header>"+"\n";
			for each(var item:EventItem in this.getChildren()){
				xml += item.toXML();
			}
			xml += "</TimeLine>"+"\n";
			System.setClipboard(xml);
			return xml;
		}
		
		private function keydownHandler(event:KeyboardEvent):void{
			var list:Array = events;
			switch(event.keyCode){
				case Keyboard.LEFT:
					move2Right();
					break;
				case Keyboard.RIGHT:
					move2Left();
					break;
			}
		}
		
		public function move2Left():void{
			//准备数据
			var evtVO:TimeLineVO = getNextEvent();
			if(evtVO){
				//添加事件到当前显示列表
				_dataProvider.push(evtVO);
				//删除当前显示列表中第一个，并放到已显示列表中
				var delVo:TimeLineVO = _dataProvider.shift();
				listLeft.push(delVo);
				//删除对应的事件对象
				removeEventObject(delVo);
				//添加显示对象到队列之后
				addEvent(evtVO,events[events.length-1].x+hgap);
				traceList("move2Left");
				//移动显示对象队列
				for each(var item:WordCupEvent in events){
					moveEvent(item,item.x-hgap);
				}
			}
		}
		
		public function move2Right():void{
			//准备数据
			var evtVO:TimeLineVO = getPriorEvent();
			if(evtVO){
				//添加事件到当前显示列表
				_dataProvider.splice(0,0,evtVO);
				//删除当前显示列表中最后一个，并放到已显示列表中
				var delVo:TimeLineVO = _dataProvider.pop();
				listRight.splice(0,0,delVo);
				//删除对应的事件对象
				removeEventObject(delVo);
				//添加显示对象到队列最前面
				addEvent(evtVO,events[0].x-hgap);
				traceList("move2Right");
				//移动显示对象队列
				for each(var item:WordCupEvent in events){
					moveEvent(item,item.x+hgap);
				}
			} 	
		}
		
		//删除指定事件对象
		private function removeEventObject(vo:TimeLineVO):void{
			var list:Array = events;
			for each(var event:WordCupEvent in list){
				if(event.timeLineVO.index == vo.index){
					this.removeChild(event);
					return;
				}
			}
		}
		
		//移动事件对象
		private function moveEvent(event:WordCupEvent,xTo:Number):void{
			var m:Move = new Move(event);
			m.xFrom = event.x;
			m.yFrom = event.y;
			m.xTo = xTo;
			m.yTo = event.y;
			m.play();
		}
		
		
		private function getPriorEvent():TimeLineVO{
			return listLeft.pop();
		}
		
		private function getNextEvent():TimeLineVO{
			return listRight.shift();
		}

		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void{
			super.updateDisplayList(unscaledWidth,unscaledHeight);
			
			_timelineTrack.y = (this.height-_timelineTrack.height)/2+_trackOffsetY;
			_timelineTrack.x = 0;
			_timelineTrack.width = this.width-2;
			
		}
		
	}
	
}